package com.turman.course.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.turman.course.CustomApplication
import com.turman.course.R
import com.turman.course.Status

class MainActivity : AppCompatActivity() {
    private lateinit var mainViewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //创建viewmodel
        mainViewModel = ViewModelProvider(this,ViewModelProvider.AndroidViewModelFactory(CustomApplication.getApplication()!!)).get(MainViewModel::class.java)

        mainViewModel.getGirls().observe(this){
            it?.let { resource->
                when(resource.status) {
                    Status.SUCCESS->{
                        Toast.makeText(this@MainActivity, "${resource.data?.status}", Toast.LENGTH_SHORT).show()
                    }
                    Status.ERROR->{
                        Toast.makeText(this@MainActivity, "${resource.message}", Toast.LENGTH_SHORT).show()
                    }
                    Status.LOADING->{
                        Toast.makeText(this@MainActivity, "loading", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }
}